package com.lyh.societiesmanager.controller;

import com.lyh.societiesmanager.ClubService.ClubService;
import com.lyh.societiesmanager.ClubService.DepartmentService;
import com.lyh.societiesmanager.ClubService.MemberService;
import com.lyh.societiesmanager.ClubService.MembershipService;
import com.lyh.societiesmanager.entity.*;
import com.lyh.societiesmanager.entity.vo.MembershipVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/memberships")
@Api(tags = "成员关系管理接口")
public class MembershipController {

    @Autowired
    private MembershipService membershipService;

    @Autowired
    private ClubService clubService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private MemberService memberService;

    @ApiOperation("查询所有成员关系")
    @GetMapping
    public Response<List<MembershipVO>> getAllMemberships() {
        // 获取所有成员关系数据
        List<Membership> membershipList = membershipService.list();

        // 收集所有clubId、memberId、departmentId，避免重复查询
        Set<Integer> clubIds = membershipList.stream().map(Membership::getClubId).collect(Collectors.toSet());
        Set<Integer> memberIds = membershipList.stream().map(Membership::getMemberId).collect(Collectors.toSet());
        Set<Integer> departmentIds = membershipList.stream().map(Membership::getDepartmentId).collect(Collectors.toSet());

        // 批量查询所需数据
        Map<Integer, Club> clubMap = clubService.listByIds(clubIds).stream()
                .collect(Collectors.toMap(Club::getClubId, club -> club));
        Map<Integer, Member> memberMap = memberService.listByIds(memberIds).stream()
                .collect(Collectors.toMap(Member::getMemberId, member -> member));
        Map<Integer, Department> departmentMap = departmentService.listByIds(departmentIds).stream()
                .collect(Collectors.toMap(Department::getDepartmentId, department -> department));

        // 构造VO列表
        List<MembershipVO> membershipVOList = membershipList.stream().map(membership -> {
            MembershipVO membershipVO = new MembershipVO();

            // 设置clubName, memberName, departmentName
            Club club = clubMap.get(membership.getClubId());
            if (club != null) {
                membershipVO.setClubName(club.getName());
            }

            Member member = memberMap.get(membership.getMemberId());
            if (member != null) {
                membershipVO.setMemberName(member.getName());
            }

            Department department = departmentMap.get(membership.getDepartmentId());
            if (department != null) {
                membershipVO.setDepartmentName(department.getName());
            }

            // 复制其他属性
            BeanUtils.copyProperties(membership, membershipVO);
            return membershipVO;
        }).collect(Collectors.toList());

        return Response.success(membershipVOList);
    }

    @ApiOperation("根据ID查询成员关系")
    @GetMapping("/{id}")
    public Response<Membership> getMembershipById(@PathVariable Integer id) {
        Membership membership = membershipService.getById(id);
        return membership != null ? Response.success(membership) : Response.error(404, "未找到该成员关系");
    }

    @ApiOperation("新增成员关系")
    @PostMapping
    public Response<Boolean> addMembership(@RequestBody Membership membership) {
        return membershipService.save(membership) ? Response.success("添加成功", true) : Response.error(500, "添加失败");
    }

    @ApiOperation("更新成员关系信息")
    @PutMapping("/{id}")
    public Response<Boolean> updateMembership(@PathVariable Integer id, @RequestBody Membership membership) {
        membership.setMembershipId(id);
        return membershipService.updateById(membership) ? Response.success("更新成功", true) : Response.error(500, "更新失败");
    }

    @ApiOperation("删除成员关系")
    @DeleteMapping("/{id}")
    public Response<Boolean> deleteMembership(@PathVariable Integer id) {
        return membershipService.removeById(id) ? Response.success("删除成功", true) : Response.error(500, "删除失败");
    }
}